package com.wm.web.action;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Restaurant;
import com.wm.bean.Shopkeeper;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.RestaurantService;
import com.wm.service.ShopkeeperService;
import com.wm.service.UserService;
import com.wm.web.interceptor.RemoteAddrAware;
import com.wm.web.servlet.ShopkeeperSession;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class ShopkeeperLogin extends BaseAction implements SessionAware, RemoteAddrAware{

	private static final Log logger = LogFactory.getLog(ShopkeeperLogin.class);

	private ShopkeeperService shopkeeperService;
	private RestaurantService restaurantService;

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	public ShopkeeperService getShopkeeperService() {
		return shopkeeperService;
	}

	public void setShopkeeperService(ShopkeeperService shopkeeperService) {
		this.shopkeeperService = shopkeeperService;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private String username;
	private String password;
	private String error;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

    
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		return "index";
	}
	
	public String login(){
		if(this.getUsername()!=null && this.getPassword()!=null){
			MD5 md5 = new MD5();
			Shopkeeper sk = this.getShopkeeperService().findShopkeeperByUserName(this.getUsername());
			if(sk == null){
				this.addFieldError("error", this.getText("error.login"));
				this.setError("1");
				return "index";
			}
			if(sk.getStatus() == 2){
				this.addFieldError("error", this.getText("error.status.disable"));
				this.setError("1");
				return "index";
			}
			if(sk.getUserPwd().equals(md5.getMD5ofStr(this.getPassword()))){
				sk.setLastIP(this.getRemoteAddr());
				sk.setLastTime(new Date());
				this.getShopkeeperService().updateShopkeeper(sk);
				ShopkeeperSession ss = new ShopkeeperSession();
				ss.setSkid(sk.getId());
				ss.setName(this.getUsername());
				ss.setStatus(sk.getStatus());
				this.getSession().put(Constant.SHOPKEEPER_SESSION_KEY,ss);
				
				//查看用户是否完成注册
				if(sk.getStatus() == 0){
					if(this.getRestaurantService().findRestaurantById(sk.getId())==null){
						//完善信息页
						this.setAction("res");
						return "regAction";
					}else{
						//等待审核页
						return "valid";
					}
				}else{
					return "restaurant";
				}
			}else{
				this.addFieldError("error", this.getText("error.login"));
				this.setError("1");
				return "index";
			}
		}
		return "index";
	}
	
	public String logout(){
		if(this.getSession().get(Constant.SHOPKEEPER_SESSION_KEY)!=null){
			this.session.remove(Constant.SHOPKEEPER_SESSION_KEY);
			if(Constant.USER_COOKIES){
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpServletResponse response = ServletActionContext.getResponse();
				UserCookie userCookie = new UserCookie(request,response);
				userCookie.removeCookie(Constant.SHOPKEEPER_SESSION_KEY);
			}
		}
		return "index";
	}
}
